import axios from 'axios';
import env from '../config/env';
import iView from 'iview';

const util = {

};
util.title = function(title) {
    title = title ? title + ' - Home' : 'OMS 企业订单管理系统';
    window.document.title = title;
};

const ajaxUrl = env === 'development' ?
    'http://172.16.7.78:8770' :
    env === 'production' ?
    'https://www.url.com' :
    'https://debug.url.com';
util.ajax = axios.create({
    baseURL: ajaxUrl,
    timeout: 30000
});

// 添加一个请求拦截器
util.ajax.interceptors.request.use(function (config) {
    // Do something before request is sent
    return config;
}, function (error) {
    // Do something with request error
    return Promise.reject(error);
});

// 添加一个响应拦截器
util.ajax.interceptors.response.use(function (response) {
    // Do something with response data
    return response;
}, function (error) {
    // Do something with response error
    iView.LoadingBar.error();
    iView.Message.error({
            content: error.response.data,
            duration: 4
        });
    return Promise.reject(error);
});

export default util;